package com.njtcedu.microblog.common.web.interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author neo
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       
        String requestUri = request.getRequestURI();
        String resource = requestUri.substring(requestUri.lastIndexOf("/"));
/*        if (handler instanceof HandlerMethod) {
            if (null == request.getSession().getAttribute("user") && !"/login".equals(resource)) {
                redirectToLoginPage(request, response);
                return false;
            }
        }*/
        return true;
    }

    private void redirectToLoginPage(HttpServletRequest request, HttpServletResponse response) {
    	String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()  + request.getContextPath();
        response.setStatus(302);
        response.setHeader("location", url + "/intranet/login");
    }

}